package com.mall.db.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.SmsCouponMapper;
import com.mall.db.mapper.SmsCouponProductCategoryRelationMapper;
import com.mall.db.mapper.SmsCouponProductRelationMapper;
import com.mall.domain.SmsCoupon;
import com.mall.domain.SmsCouponProductCategoryRelation;
import com.mall.domain.SmsCouponProductRelation;
import com.mall.interface1.api.SmsCouponApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsCouponApiImpl implements SmsCouponApi {

    @Autowired
    private SmsCouponMapper smsCouponMapper;

    @Autowired
    private SmsCouponProductCategoryRelationMapper smsCouponProductCategoryRelationMapper;

    @Autowired
    private SmsCouponProductRelationMapper smsCouponProductRelationMapper;

    //存入优惠券表并返回优惠券id
    public Long addCoupon(SmsCoupon smsCoupon) {
        smsCouponMapper.insert(smsCoupon);
        return smsCoupon.getId();
    }

    @Override
    public void addScpcr(SmsCouponProductCategoryRelation smsCouponProductCategoryRelation) {
        if (smsCouponProductCategoryRelation != null){
            smsCouponProductCategoryRelationMapper.insert(smsCouponProductCategoryRelation);
        }
    }

    @Override
    public void addScpr(SmsCouponProductRelation smsCouponProductRelation) {
        if(smsCouponProductRelation != null){
            smsCouponProductRelationMapper.insert(smsCouponProductRelation);
        }
    }

    @Override
    public void deleteCouponById(Long id) {
        smsCouponMapper.deleteById(id);
    }

    @Override
    public void deletescpcr(Long id) {
        LambdaQueryWrapper<SmsCouponProductCategoryRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,SmsCouponProductCategoryRelation::getCouponId,id);
        smsCouponProductCategoryRelationMapper.delete(queryWrapper);
    }

    @Override
    public void deletescpr(Long id) {
        LambdaQueryWrapper<SmsCouponProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,SmsCouponProductRelation::getCouponId,id);
        smsCouponProductRelationMapper.delete(queryWrapper);
    }

    //根据条件分页查询出优惠券列表
    public List<SmsCoupon>  list(String name, Integer pageNum, Integer pageSize, Integer type) {
        Page<SmsCoupon> couponPage = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsCoupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,SmsCoupon::getName,name);
        queryWrapper.eq(type != null,SmsCoupon::getType,type);
        Page<SmsCoupon> page = smsCouponMapper.selectPage(couponPage, queryWrapper);
        return page.getRecords();
    }

    //查询符合条件总记录条数
    public Integer count(String name, Integer pageNum, Integer pageSize, Integer type) {
        LambdaQueryWrapper<SmsCoupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,SmsCoupon::getName,name);
        queryWrapper.eq(type != null,SmsCoupon::getType,type);
        return smsCouponMapper.selectCount(queryWrapper);
    }

    //修改优惠券表
    public void update(Long id, SmsCoupon smsCoupon) {
        smsCouponMapper.updateById(smsCoupon);
    }

    //修改优惠券相关表1
    public void updateScpcr(Long id, SmsCouponProductCategoryRelation smsCouponProductCategoryRelation) {
        LambdaQueryWrapper<SmsCouponProductCategoryRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(smsCouponProductCategoryRelation != null,SmsCouponProductCategoryRelation::getId,smsCouponProductCategoryRelation.getId());
        queryWrapper.eq(id != null,SmsCouponProductCategoryRelation::getCouponId,id);
        smsCouponProductCategoryRelationMapper.update(smsCouponProductCategoryRelation,queryWrapper);
    }

    //修改优惠券相关表2
    public void updateScpr(Long id, SmsCouponProductRelation smsCouponProductRelation) {
        LambdaQueryWrapper<SmsCouponProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(smsCouponProductRelation != null,SmsCouponProductRelation::getId,smsCouponProductRelation.getId());
        queryWrapper.eq(id != null,SmsCouponProductRelation::getCouponId,id);
        smsCouponProductRelationMapper.update(smsCouponProductRelation,queryWrapper);
    }

    //获取单个优惠券的详细信息
    public SmsCoupon findById(Long id) {
        return smsCouponMapper.selectById(id);
    }

    //获取单个优惠券的相关信息1
    public List<SmsCouponProductCategoryRelation> findScpcr(Long id) {
        LambdaQueryWrapper<SmsCouponProductCategoryRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null, SmsCouponProductCategoryRelation::getCouponId, id);
        return smsCouponProductCategoryRelationMapper.selectList(queryWrapper);
    }

    //获取单个优惠券的相关信息2
    public List<SmsCouponProductRelation> findScpr(Long id) {
        LambdaQueryWrapper<SmsCouponProductRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(id != null,SmsCouponProductRelation::getCouponId,id);
        return smsCouponProductRelationMapper.selectList(queryWrapper);
    }
}
